package cc.lzmsh.service.impl;

import cc.lzmsh.domain.dto.RegisterDTO;
import cc.lzmsh.domain.dto.UserInfoDTO;
import cc.lzmsh.domain.entity.User;
import cc.lzmsh.domain.result.Result;
import cc.lzmsh.domain.vo.UserInfoVo;
import cc.lzmsh.enums.AppHttpCodeEnum;
import cc.lzmsh.exception.SystemException;
import cc.lzmsh.mapper.UserMapper;
import cc.lzmsh.service.UserService;
import cc.lzmsh.utils.BeanCopyUtils;
import cc.lzmsh.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 用户表(User)表服务实现类
 *
 * @author msh
 * @since 2024-09-15 20:17:06
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Result<UserInfoVo> userInfo() {
        // 获取当前用户id
        Long userId = SecurityUtils.getUserId();
        // 查询用户信息
        User user = getById(userId);
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        return Result.success(userInfoVo);
    }

    @Override
    public Result updateUserInfo(UserInfoDTO userInfoDTO) {
        User user = BeanCopyUtils.copyBean(userInfoDTO, User.class);
        updateById(user);
        return Result.success();
    }

    @Override
    public Result register(RegisterDTO registerDTO) {
        // 对数据进行非空校验
        if (!StringUtils.hasText(registerDTO.getUserName()))
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        if (!StringUtils.hasText(registerDTO.getPassword()))
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
        if (!StringUtils.hasText(registerDTO.getEmail()))
            throw new SystemException(AppHttpCodeEnum.EMAIL_NOT_NULL);
        if (!StringUtils.hasText(registerDTO.getNickName()))
            throw new SystemException(AppHttpCodeEnum.NICKNAME_NOT_NULL);
        // 判断用户名不能重复
        if (userNameExist(registerDTO.getUserName()))
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        // 判断昵称是否重复
        if (nickNameExist(registerDTO.getNickName()))
            throw new SystemException(AppHttpCodeEnum.NICKNAME_EXIST);

        // 对密码进行加密
        String encodePassword = passwordEncoder.encode(registerDTO.getPassword());
        User user = BeanCopyUtils.copyBean(registerDTO, User.class);
        user.setPassword(encodePassword);
        save(user);
        return Result.success();
    }

    private boolean nickNameExist(String nickName) {
        return getOne(new QueryWrapper<User>().eq("nick_name", nickName)) != null;
    }

    private boolean userNameExist(String userName) {
        return getOne(new QueryWrapper<User>().eq("user_name", userName)) != null;
    }
}

